package com.qys.livesMall.shortVideo.controller.sys;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.framework.view.BaseQuery;
import com.qys.livesMall.shortVideo.model.entity.H2ShortVideo;
import com.qys.livesMall.shortVideo.model.entity.H2ShortVideoCategory;
import com.qys.livesMall.shortVideo.service.IH2ShortVideoCategoryService;
import com.qys.livesMall.shortVideo.service.IH2ShortVideoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 短视频 前端控制器
 * </p>
 *
 * @author dp
 * @since 2023-05-04
 */
@RestController
@RequestMapping("/sys/short/video/category")
@Tag(name = "后台管理-短视频分类")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class H2ShortVideoCategoryController {

    private final IH2ShortVideoCategoryService shortVideoCategoryService;
    private final IH2ShortVideoService shortVideoService;

    @PostMapping("/page")
    @Operation(summary = "分页查询分类信息")
    public R<Page<H2ShortVideoCategory>> page(@RequestBody BaseQuery queryDTO) {
        return R.ok(shortVideoCategoryService.page(queryDTO));
    }

    @PostMapping("/insert")
    @Operation(summary = "新增分类信息")
    public R<Void> insertCategory(@RequestBody @Validated H2ShortVideoCategory videoCategory) {
        shortVideoCategoryService.save(videoCategory);
        return R.ok();
    }

    @PutMapping("/edit")
    @Operation(summary = "修改分类信息")
    public R<Void> editCategory(@RequestBody @Validated H2ShortVideoCategory videoCategory) {
        shortVideoCategoryService.updateById(videoCategory);
        return R.ok();
    }

    @DeleteMapping("/remove")
    @Operation(summary = "根据id删除分类信息")
    public R<Void> removeByIds(@RequestBody List<String> ids) {
        List<H2ShortVideo> videoList = shortVideoService.list(Wrappers.<H2ShortVideo>query().in("short_video_category_id", ids));
        if (CollectionUtils.isNotEmpty(videoList)) {
            return R.fail("已有短视频关联分类信息,不能删除");
        }

        shortVideoCategoryService.removeByIds(ids);
        return R.ok();
    }
}
